import logger from '../core/logger';
import apiInfo from './core/apiInfo';
import apiProvider from './core/apiProvider';
import { clientBase } from './core/clientBase';
import returnModel from '../model/common/returnModel';
import videoInfoModels from '../model/videos/videoInfoModels';

class playerClient  extends clientBase {
  constructor() {
    super();
  }

  getVideoInfo(avId: string): Promise<returnModel<videoInfoModels>> {
    return new Promise((resolve) => {
      try {
        let info = new apiInfo("https://app.bilibili.com/x/v2/view", `&aid=${avId}&plat=0`);
        this.client.getResult(info).then((res) => {
          let data: videoInfoModels = res;
          resolve(this.buildSuccessResult<videoInfoModels>(data));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<videoInfoModels>(e));
      }
    });
  }

  getBiliPlus(avId: string): Promise<returnModel<videoInfoModels>> {
    return new Promise((resolve) => {
      try {
        let url = `https://www.biliplus.com/api/view?id=${avId}&access_key=${apiProvider.accessKey}`;
        this.client.getResult(url).then((res) => {
          let data: videoInfoModels = res.data.v2_app_api;
          resolve(this.buildSuccessResult<videoInfoModels>(data));
        });
      } catch (e) {
        logger.error(e);
        resolve(this.buildExceptionResult<videoInfoModels>(e));
      }
    });
  }
}
export default new playerClient();